package com.jxmcloud.business.cut.service.impl.user;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.json.DataConfigService;
import com.jxmcloud.business.cut.service.user.IUserBaseService;
import com.jxmcloud.business.cut.service.user.IUserCashDetailListService;
import com.jxmcloud.business.cut.service.user.IUserOrderService;
import com.jxmcloud.business.cut.util.VipRpcUtil;
import com.jxmcloud.business.cut.vo.UserOrderTranVo;
import com.jxmcloud.business.cut.vo.json.ConfigProgressVo;
import com.jxmcloud.business.cut.vo.json.ConfigStartVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserCashDetailItemVo;
import com.jxmcloud.business.cut.vo.vip.UserOrderCountVo;
import com.xlcloud.platform.api.config.Constant;
import com.xlcloud.platform.util.DateUtil;
import com.xlcloud.platform.util.PublicUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Lazy
public class UserOrderServiceImpl implements IUserOrderService {

    @Resource
    private DataConfigService dataConfigService;
    @Resource
    private VipRpcUtil vipRpcUtil;
    @Resource
    private IUserBaseService userBaseService;
    @Resource
    private IUserCashDetailListService userCashDetailListService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveToDb(PlayerInfoVo player, String key) {

        return true;
    }


    @Override
    @Async
    public boolean loadFromDb(PlayerInfoVo player, Integer uid) {
        return true;
    }
    public boolean selectVipOrder(PlayerInfoVo player) {
        //读取开局数据
        ConfigStartVo startConfig = dataConfigService.getConfigStartConfig(player.getActivity().getStartId());
        if(PublicUtil.isEmpty(startConfig)){
            return false;
        }
        UserOrderCountVo userRecentBuyList = vipRpcUtil.getUserRecentBuyList(userBaseService.getUserBase(player).getAccount(), 1, startConfig.getGetOrderCount()
                , DateUtil.subtractTimeall(DateUtil.newDate(), 3, "2"));
        if(ObjectUtil.isNotEmpty(userRecentBuyList) && ObjectUtil.isNotEmpty(userRecentBuyList.getUserOrderVoList())){
            List<UserOrderTranVo> userOrderTranVos = userRecentBuyList.getUserOrderVoList().stream().map(d -> {
                UserOrderTranVo userOrderTranVo = new UserOrderTranVo();
                userOrderTranVo.setOrderId(d.getGoodsOrderId());
                userOrderTranVo.setUid(player.getBase().getUid());
                userOrderTranVo.setActivityId(player.getActivity().getActivityId());
                userOrderTranVo.setUsed(0);
                userOrderTranVo.setImgUrl(d.getImgUrl());
                userOrderTranVo.setGoodsName(d.getGoodsName());
                userOrderTranVo.setSpecId(d.getSpecId());
                return userOrderTranVo;
            }).collect(Collectors.toList());
            player.getUserOrderListVo().setUserOrderList(userOrderTranVos);
        }
        return true;
    }

    @Override
    public boolean selectVipOrderNum(PlayerInfoVo player) {
        Integer userTotalBuyNum = vipRpcUtil.getUserTotalBuyNum(userBaseService.getUserBase(player).getAccount()
                , DateUtil.subtractTimeall(DateUtil.newDate(), 3, "2"));
        player.getUserOrderListVo().setTotalBuyNum(userTotalBuyNum);
        return true;
    }

    @Override
    @Async
    public boolean register(PlayerInfoVo player, Integer uid) {

        return true;
    }

    // ==================================

    /**
     * 检查 并获取 流程
     *
     * @param progressId
     * @return 流程详情
     */
    @Override
    public ConfigProgressVo checkProgressDto(Integer progressId) {
        return dataConfigService.getConfigProgressConfig(Math.toIntExact(progressId));
    }

    @Override
    public UserOrderTranVo updateUserOrders(PlayerInfoVo player, Integer progressId) {
        //获取流程对应的转换信息
        UserCashDetailItemVo itemVo = userCashDetailListService.getCashDetailItemByProgressId(player, progressId);
        if(ObjectUtil.isNull(itemVo)){
            return null;
        }
        return getUserOrderTranVo(player, itemVo.getOrderId(), itemVo.getSpecId());
    }

    public UserOrderTranVo getUserOrderTranVo(PlayerInfoVo player, Integer orderId, Integer specId) {
        List<UserOrderTranVo> userOrderList = player.getUserOrderListVo().getUserOrderList();
        //获取对应的订单信息
        List<UserOrderTranVo> orderTranVoList = userOrderList.stream()
                .filter(d -> (orderId.equals(d.getOrderId())) && (specId.equals(d.getSpecId())))
                .collect(Collectors.toList());
        if(ObjectUtil.isEmpty(orderTranVoList)){
            return null;
        }
        return PublicUtil.first(orderTranVoList);
    }

    /**
     * 处理有订单
     *
     * @param userOrderList
     * @param orderId
     * @return
     */
    @Override
    public UserOrderTranVo updateSpecificOrder(PlayerInfoVo player, List<UserOrderTranVo> userOrderList, Integer orderId) {
        for (UserOrderTranVo order : userOrderList) {
            if (Objects.equals(order.getOrderId(), orderId)) {
                if (Objects.equals(order.getUsed(), Constant.INT_ZERO)) {
                    order.setUsed(Constant.INT_ONE);
                    return order;
                }
            }
        }
        return null; // 未找到对应订单
    }

    /**
     * 处理误订单
     *
     * @param userOrderList
     * @return
     */
    @Override
    public UserOrderTranVo updateFirstUnusedOrder(PlayerInfoVo player, List<UserOrderTranVo> userOrderList) {
        for (UserOrderTranVo order : userOrderList) {
            if (Objects.equals(order.getUsed(), Constant.INT_ZERO)) {
                order.setUsed(Constant.INT_ONE);
                return order;
            }
        }
        return null; // 无未使用订单
    }

    @Override
    public void resetAllOrders(PlayerInfoVo player) {
        List<UserOrderTranVo> orderList =  player.getUserOrderListVo().getUserOrderList();
        for (UserOrderTranVo order : orderList) {
            order.setUsed(Constant.INT_ZERO);
        }
    }
}
